<?php

require 'defaultController.php';

class indexController extends defaultController {

    function indexAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        $id = $_SESSION['userid'];
        $data['info'] = $this->_model('login')->getinfo($id);
        $data['count1'] = $this->_model('task')->allsum($id);
        $data['count2'] = $this->_model('task')->allsum2($id);
        $data['count3'] = $this->_model('task')->allsum3($id);
        $data['pai1']= $this->_model('task')->pai1();
        $data['pai2']= $this->_model('login')->pai2();
        $pai3= $this->_model('login')->pai3();
        $arr=array();
        foreach ($pai3 as $val){
            if($val['friend']<>0){
                $arr[$val['friend']]=$val['COUNT(*)'];
            }  
        }

        $this->_view('index.tpl.php', $data);
    }

    function artlistAction() {
        $result = $this->_model('art')->getallart1(1);
        $data['artlist'] = $result;
        
        $result2 = $this->_model('art')->getallart1(2);
        $data['artlist2'] = $result2;
        
        $result3 = $this->_model('art')->getallart1(3);
        $data['artlist3'] = $result3;
        
        $this->_view('news.tpl.php', $data);
    }
    function artlist1Action() {
        $result = $this->_model('art')->getallart1(1);
        $data['artlist'] = $result;
        
        $result2 = $this->_model('art')->getallart1(2);
        $data['artlist2'] = $result2;
        
        $result3 = $this->_model('art')->getallart1(3);
        $data['artlist3'] = $result3;
        
        $this->_view('news1.tpl.php', $data);
    }
    function artlist2Action() {
        $result = $this->_model('art')->getallart1(1);
        $data['artlist'] = $result;
        
        $result2 = $this->_model('art')->getallart1(2);
        $data['artlist2'] = $result2;
        
        $result3 = $this->_model('art')->getallart1(3);
        $data['artlist3'] = $result3;
        
        $this->_view('news2.tpl.php', $data);
    }
    function artlist3Action() {
        $result = $this->_model('art')->getallart1(1);
        $data['artlist'] = $result;
        
        $result2 = $this->_model('art')->getallart1(2);
        $data['artlist2'] = $result2;
        
        $result3 = $this->_model('art')->getallart1(3);
        $data['artlist3'] = $result3;
        
        $this->_view('news3.tpl.php', $data);
    }

    function readAction() {
        $id = $this->for_get('id');
        $result = $this->_model('art')->getoneart($id);
        $data['art'] = $result;
        $data['back'] = $this->_model('backmess')->getallart($id);
        $this->_view('read.tpl.php', $data);
    }

    function mymessAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        if ($this->for_post('submit')) {
            $data = array(
                'relname' => $this->for_post('relname'),
                'email' => $this->for_post('email'),
                'phone' => $this->for_post('phone')
            );
            $result = $this->_model('login')->updatemember($data);
            if ($result) {
                $data = array(
                    'title' => "修改账号资料",
                    'time' => date("Y/m/d"),
                    'member_id' => $_SESSION['userid'],
                    'class' => 2
                );
                $this->_model('data')->add($data);
                $this->redirectPage("index.php?c=index&a=index");
            }
        }
        $id = $_SESSION['userid'];
        $info = $this->_model('login')->getinfo($id);
        $data['info'] = $info;
        $this->_view('my.tpl.php', $data);
    }

    function myinfoAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        $id = $_SESSION['userid'];
        $info = $this->_model('login')->getinfo($id);
        $data['info'] = $info;
        $this->_view('myinfo.tpl.php', $data);
    }

    function messlistAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        $id = $_SESSION['userid'];
        $list = $this->_model('mess')->memberlist($id);
        $data['list'] = $list;
        $this->_view('messlist.tpl.php', $data);
    }
    

    function addmoneyAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        if($this->for_post('submit')){
            $id=$_SESSION['userid'];
            $money=$this->for_post('money');
            $taobao=$this->for_post('taobao');
            if(!($money&&$taobao))$this->showMessage ('信息填写错误');
            $data=array(
                'name'=>$taobao,
                'money'=>$money,
                'member_id'=>$id
            );
            $result=$this->_model('taobao')->add($data);
            if($result) $this->showMessage ('充值'.$money.'成功');
        }else{
            $list=$this->_model('taobao')->all($_SESSION['userid']);
            $data['list']=$list;
            $this->_view('addmoney.tpl.php',$data);
        }
        
    }

    function myfriendAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        $id = $_SESSION['userid'];
        $friend = $this->_model('login')->friend($id);
        $data['friend'] = $friend;
        $data['mrank'] = $this->_model('login')->getinfo($id)['mrank'];
        $data['data']=$this->_model('data')->mlist4($id);
        $this->_view('myfriend.tpl.php', $data);
    }
    
    function friendmAction(){
        $rank=$this->for_post('rank');
        $money=$rank*0.6;
        $id = $_SESSION['userid'];
        $info = $this->_model('login')->getinfo($id);
        if($rank>$info['mrank']){
            $this->showMessage('你的积分不足！');
        }else{
             $rank2=$info['mrank']-$rank;
             $money2=$money+$info['money'];
             $data=array(
                 'mrank'=>$rank2,
                 'money'=>$money2
             );
             $result=$this->_model('login')->updatemember($data);
             $data = array(
                    'title' =>$rank."推广积分兑换".$money."元",
                    'time' => date("Y/m/d"),
                    'member_id' => $_SESSION['userid'],
                    'class' => 4
                );
                $this->_model('data')->add($data);
             if($result)$this->showMessage('兑换成功！',"index.php?c=index&a=myinfo",5000);
        }
       
        
    }

    function backmoneyAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        $id = $_SESSION['userid'];
        $info = $this->_model('login')->getinfo($id);

        if ($this->for_post('submit')) {
            if ($info['money'] >= $this->for_post('backmoney') && $this->for_post('alipay')) {
                $data = array(
                    'member_id' => $id,
                    'money' => $this->for_post('backmoney'),
                    'alipay' => $this->for_post('alipay'),
                    'status' => 1
                );
                $this->_model('backmoney')->add($data);
                $member = $this->_model('login')->getinfo($id);
                $money = $member['money'] - $this->for_post('backmoney');
                $data = array(
                    'money' => $money
                );
                $result = $this->_model('login')->updatemember($data);
                if ($result) {
                    $data = array(
                        'title' => "提交提现" . $this->for_post('backmoney'),
                        'time' => date("Y/m/d"),
                        'member_id' => $_SESSION['userid'],
                        'class' => 3
                    );
                    $this->_model('data')->add($data);
                    $this->showMessage('提交成功！');
                }
            } else {
                $this->showMessage('提现提交错误！');
            }
        }
        $data['info'] = $info;
        $data['list'] = $this->_model('backmoney')->all($id);
        $this->_view('backmoney.tpl.php', $data);
    }

    function upassAction() {
        if (!$this->checkLogin()) {
            $this->redirectPage("index.php?c=login&a=index");
        }
        if($this->for_post("submit")){
            if($this->for_post("password")== $this->for_post("password2")&&$this->for_post("password")!==''){
              $data = array(
                  'password' => $this->for_post('password')
                );
              $id = $_SESSION['userid'];
              $result=$this->_model('login')->updatemember($data);
              if($result){
                  $this->redirectPage("index.php?c=login&a=loginout");
              }
            }else{
                $this->showMessage('两次密码不一致！');
            }    
        }              
        $this->_view('upass.tpl.php');
    }

}
